/**
 * 
 */
package com.wegax.finprod.accountcard;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vaadin.teemu.wizards.event.WizardCancelledEvent;
import org.vaadin.teemu.wizards.event.WizardCompletedEvent;
import org.vaadin.teemu.wizards.event.WizardProgressListener;
import org.vaadin.teemu.wizards.event.WizardStepActivationEvent;
import org.vaadin.teemu.wizards.event.WizardStepSetChangedEvent;

import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;
import com.wegax.finprod.ApplicationFormsController;
import com.wegax.finprod.TitledForm;
import com.wegax.finprod.infrastructure.GlobalContext;
import com.wegax.finprod.message.MessageCodes;
import com.wegax.finprod.message.Messages;
import com.wegax.finprod.sender.ApplicationFormSender;
import com.wegax.finprod.sender.SendingException;
import com.wegax.finprod.wizardform.WizardForm;

/**
 * @author wegax
 * 
 */
public class PersonalAccountDebitCardView extends VerticalLayout implements TitledForm {

	private static final Logger log = LoggerFactory.getLogger(PersonalAccountDebitCardView.class);

	private final ApplicationFormsController applicationFormsController;

	private PadcWizardForm personalAccountDebitCardWizard;

	public PersonalAccountDebitCardView(ApplicationFormsController applicationFormsController) {
		this.applicationFormsController = applicationFormsController;
		setWidth("50em");
		initComponents();
	}

	private void initComponents() {
		personalAccountDebitCardWizard = new PadcWizardForm();
		personalAccountDebitCardWizard.addFormStep(new PersonalInformationWizardStep());
		personalAccountDebitCardWizard.addFormStep(new AddressInformationWizardStep());
		personalAccountDebitCardWizard.addFormStep(new ProductInformationWizardStep());
		personalAccountDebitCardWizard.addFormStep(new SummaryWizardStep());

		clearDefualtHeader(personalAccountDebitCardWizard);
		addLifeCycleListener(personalAccountDebitCardWizard);

		addComponent(personalAccountDebitCardWizard);
	}

	/**
	 * @param personalAccountDebitCardWizard
	 */
	private void addLifeCycleListener(final WizardForm personalAccountDebitCardWizard) {
		personalAccountDebitCardWizard.addListener(new WizardProgressListener() {

			@Override
			public void wizardCompleted(WizardCompletedEvent event) {
				PersonalAccountDebitCardView.this.onWizardCompleted(personalAccountDebitCardWizard);
			}

			@Override
			public void wizardCancelled(WizardCancelledEvent event) {
				applicationFormsController.cancel();
			}

			@Override
			public void stepSetChanged(WizardStepSetChangedEvent event) {
				log.debug("stepSetChanged event");
			}

			@Override
			public void activeStepChanged(WizardStepActivationEvent event) {
				log.debug("activeStepChanged event");
			}
		});
	}

	/**
	 * @param personalAccountDebitCardWizard
	 */
	private void onWizardCompleted(final WizardForm personalAccountDebitCardWizard) {
		try {
			String applicationIdentifier = GlobalContext.getBean(ApplicationFormSender.class).sendApplication(
					personalAccountDebitCardWizard.getResultMapper());
			handleSuccessSending(applicationIdentifier);
		} catch (SendingException e) {
			handleApplicationSendingError(e);
		} catch (IllegalStateException e) {
			handleApplicationSendingError(e);
		}
	}

	/**
	 * @param applicationIdentifier
	 */
	private void handleSuccessSending(String applicationIdentifier) {
		Window infoWindow = new Window(Messages.getString(MessageCodes.SENDING_SUCCESS_CAPTION));
		infoWindow.setModal(true);
		infoWindow.addComponent(new Label(Messages.getString(MessageCodes.SENDING_SUCCESS_DESCRIPTION,
				applicationIdentifier)));
		getWindow().addWindow(infoWindow);
		applicationFormsController.afterSend();
	}

	/**
	 * @param e
	 */
	private void handleApplicationSendingError(Exception e) {
		log.warn(e.getMessage(), e);
		getWindow().showNotification(Messages.getString(MessageCodes.SENDING_ERROR_CAPTION),
				Messages.getString(MessageCodes.SENDING_ERROR_DESCRIPTION), Notification.TYPE_WARNING_MESSAGE);
	}

	/**
	 * @param personalAccountDebitCardWizard
	 */
	private void clearDefualtHeader(final WizardForm personalAccountDebitCardWizard) {
		Component defaultHeader = personalAccountDebitCardWizard.getHeader();
		if (defaultHeader instanceof WizardProgressListener) {
			personalAccountDebitCardWizard.removeListener((WizardProgressListener) defaultHeader);
		}
		personalAccountDebitCardWizard.setHeader(null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.TitledForm#getTitle()
	 */
	@Override
	public String getTitle() {
		return Messages.getString(MessageCodes.PERSONAL_ACCOUNT_DEBIT_CARD_APPLICATION_FORM_TITLE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.TitledForm#getComponentContainer()
	 */
	@Override
	public ComponentContainer getComponentContainer() {
		return this;
	}

}
